package com.lyn.solitude.pluginhost.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Resources
import android.os.Bundle
import android.os.Process
import android.util.Log
import android.view.ContextMenu
import android.view.ContextMenu.ContextMenuInfo
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.lyn.solitude.api.PluginLifecycleApi
import com.lyn.solitude.pluginhost.binder.PluginHostBinder
import com.lyn.solitude.pluginhost.factory.ClassLoaderFactory
import com.lyn.solitude.pluginhost.factory.ResourcesFactory
import com.lyn.solitude.pluginhost.stack.PluginStack

/**
 * 插件代理activity
 *
 * @author longyn 2024/4/14
 * @version 1.0.0
 */
class PluginProxyActivity : AppCompatActivity() {

    companion object {
        const val TAG = "PluginProxyActivity"
        const val PLUGIN_ACTIVITY_CLASS_NAME_KEY = "pluginActivityClassName"
    }
    lateinit var pluginLifecycleApi: PluginLifecycleApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       try {
           val plugin = PluginStack.peek()!!
           val className = intent.getStringExtra(PLUGIN_ACTIVITY_CLASS_NAME_KEY)
           val pluginClass = getClassLoader().loadClass(className)
           pluginLifecycleApi =
               pluginClass.getDeclaredConstructor().newInstance() as PluginLifecycleApi
           pluginLifecycleApi.onPluginHostAidl(PluginHostBinder)
           pluginLifecycleApi.onPlugin(plugin)
           pluginLifecycleApi.onActivity(this)
           pluginLifecycleApi.onCreate(savedInstanceState)
       }catch (e:Throwable){
           e.printStackTrace()
           finish()
       }finally {
           Log.d(TAG,"当前进程:"+Process.myPid())

       }
    }


    override fun getClassLoader(): ClassLoader {
        val plugin = PluginStack.peek()
        return ClassLoaderFactory.get(plugin!!.filePath)!!
    }

    override fun getResources(): Resources {
        val plugin = PluginStack.peek()
        return ResourcesFactory.get(plugin!!.filePath)!!
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        return pluginLifecycleApi.onCreateOptionsMenu(menu)
    }

    override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenuInfo) {
        pluginLifecycleApi.onCreateContextMenu(menu, v, menuInfo)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return pluginLifecycleApi.onOptionsItemSelected(item)
    }

    override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
        return pluginLifecycleApi.onMenuOpened(featureId, menu)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        pluginLifecycleApi.onNewIntent(intent)
    }

    override fun onRestart() {
        super.onRestart()
        pluginLifecycleApi.onRestart()
    }

    override fun onStart() {
        super.onStart()
        pluginLifecycleApi.onStart()
    }

    override fun onResume() {
        super.onResume()
        pluginLifecycleApi.onResume()
    }

    override fun onPause() {
        super.onPause()
        pluginLifecycleApi.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        pluginLifecycleApi.onDestroy()
        PluginStack.pop()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        pluginLifecycleApi.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    @Deprecated("Deprecated in Java")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        pluginLifecycleApi.onActivityResult(requestCode, resultCode, data)
    }

    @Deprecated("Deprecated in Java")
    @SuppressLint("MissingSuperCall")
    override fun onBackPressed() {
        if (pluginLifecycleApi.isNotBackPressed()) {
            return
        }
        onBackPressedDispatcher.onBackPressed()
    }


}
